<template>
    <transition name="fade">
        <div v-if="visible" class="custom-image-preview" @click.self="close">
            <div class="preview-container">
                <span class="close-btn" @click="close">&times;</span>
                <img :src="currentImage" class="preview-image" />
            </div>
        </div>
    </transition>
</template>

<script>
export default {
    props: {
        imageUrl: {
            type: String,
            required: true
        }
    },
    data() {
        return {
            visible: false
        }
    },
    computed: {
        currentImage() {
            console.log(this.imageUrl);
            return this.imageUrl
        }
    },
    methods: {
        open() {
            this.visible = true
            document.body.style.overflow = 'hidden'
            document.addEventListener('keydown', this.handleKeyDown)
        },
        close() {
            this.visible = false
            document.body.style.overflow = ''
            document.removeEventListener('keydown', this.handleKeyDown)
        },
        handleKeyDown(e) {
            if (e.key === 'Escape') {
                this.close()
            }
        }
    },
    beforeUnmount() {
        document.removeEventListener('keydown', this.handleKeyDown)
        document.body.style.overflow = ''
    }
}
</script>

<style scoped>
.fade-enter-active,
.fade-leave-active {
    transition: opacity 0.3s;
}

.fade-enter-from,
.fade-leave-to {
    opacity: 0;
}

.custom-image-preview {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 99999;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: rgba(255, 255, 255, 0.8);
    backdrop-filter: blur(2px);
}

.preview-container {
    position: relative;
    max-width: 90%;
    max-height: 90%;
    display: flex;
    justify-content: center;
    align-items: center;
}

.preview-image {
    max-width: 90vw;
    max-height: 90vh;
    object-fit: contain;
    border-radius: 4px;
    box-shadow: 0 2px 30px rgba(0, 0, 0, 0.3);
}

.close-btn {
    position: absolute;
    top: -40px;
    right: 0;
    font-size: 30px;
    cursor: pointer;
    color: white;
    background: rgba(0, 0, 0, 0.5);
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: all 0.3s;
}

.close-btn:hover {
    background: rgba(255, 255, 255, 0.8);
    color: #333;
}
</style>